package tp.pr5.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tp.pr5.Constants;
import tp.pr5.RobotEngineObserver;
import tp.pr5.Rotation;

public class InstructionPanel extends JPanel implements RobotEngineObserver
{
	private static final long serialVersionUID = 1L;
	
	private JButton move;
	private JButton turn;
	private JButton pick;
	private JButton drop;
	private JButton quit;
	private JButton operate;
	private JButton storm;
	private JButton defuse;
	private JButton shop;
	private JButton trigger;
	
	
	private JComboBox<Rotation> comboBox;
	
	private JTextField textField;
	
	private GUIController gameController;

	private RobotPanel robotPanel;

	/**
	 * Main Constructor
	 */
	public InstructionPanel(GUIController gameController, RobotPanel robotPanel)
	{
		super();
		this.gameController = gameController;
		this.robotPanel = robotPanel;
		this.move = new JButton("MOVE");
		this.turn = new JButton("TURN");
		this.pick = new JButton("PICK");
		this.drop = new JButton("DROP");
		this.quit = new JButton("QUIT");
		this.operate = new JButton("OPERATE");
		this.storm = new JButton("STORM");
		this.defuse = new JButton("DEFUSE");
		this.shop = new JButton("SHOP");
		this.trigger = new JButton("TRIGGER");
		this.comboBox = new JComboBox<Rotation>(new Rotation[] { Rotation.LEFT, Rotation.RIGHT });
		this.textField = new JTextField();
		build();
	}
	
	/**
	 * Builds the user interface
	 * 
	 */
	public void build()
	{
		this.setBorder(BorderFactory.createTitledBorder("Instructions"));
		this.setLayout(new GridLayout(6, 2, 5, 5));
		this.add(this.move);
		this.add(this.quit);
		this.add(this.turn);
		this.add(this.comboBox);
		this.add(this.pick);
		this.add(this.textField);
		this.add(this.drop);
		this.add(this.operate);
		this.add(this.shop);
		this.add(this.trigger);
		this.add(this.storm);
		this.add(this.defuse);
		
		//ACTION LISTENER FOR TRIGGER INSTRUCTION
		this.trigger.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) 
			{
				String triggerText = InstructionPanel.this.textField.getText();
				
				if(!triggerText.isEmpty())
				{
					gameController.executeTriggerAction(triggerText);
				}
				else
				{
					JOptionPane.showMessageDialog(null, Constants.MESSAGE_NO_ITEM_SELECTED);
				}
			}
		});
		
		//ACTION LISTENER FOR DEFUSE INSTRUCTION
				this.defuse.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) 
					{
						String defuseText = InstructionPanel.this.textField.getText();
						
						if(!defuseText.isEmpty())
						{
							gameController.executeDefuseAction(defuseText);
						}
						else
						{
							JOptionPane.showMessageDialog(null, Constants.MESSAGE_NO_ITEM_SELECTED);
						}
					}
				});
		
		//ACTION LISTENER FOR STORM INSTRUCTION
		this.storm.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) 
			{
				gameController.executeStormAction();
			}
		});
		
		//ACTION LISTENER FOR QUIT INSTRUCTION
		this.quit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0)
			{
				int close = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit", JOptionPane.YES_NO_OPTION);

				if(close == JOptionPane.YES_OPTION)
				{
					gameController.executeQuitAction();
				}
				
			}
		});
		
		//ACTION LISTENER FOR TURN INSTRUCTION
		this.turn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) 
			{
				Rotation rot = (Rotation) InstructionPanel.this.comboBox.getSelectedItem();
				gameController.executeTurn(rot);
			}
		});
		
		//ACTION LISTENER FOR MOVE INSTRUCTION
		this.move.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) 
			{
				gameController.executeMoveAction();			
			}
		});
		
		//ACTION LISTENER FOR PICK INSTRUCTION
		this.pick.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) 
			{
				String pickText = InstructionPanel.this.textField.getText();
				
				if(!pickText.isEmpty())
				{
					gameController.executePickAction(pickText);
				}
				else
				{
					JOptionPane.showMessageDialog(null, Constants.MESSAGE_NO_ITEM_SELECTED);
				}
				
			}
		});
		
		//ACTION LISTENER FOR OPERATE INSTRUCTION
		this.operate.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) 
			{
				String item = robotPanel.getSelectedItem();
					
				if(item != null)
				{
					gameController.executeOperateAction(item);
				}
				else
				{
					JOptionPane.showMessageDialog(null, Constants.MESSAGE_NO_ITEM_SELECTED);
				}
			}
		});
		
		//ACTION LISTENER FOR DROP INSTRUCTION
		this.drop.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) 
			{
				String item = robotPanel.getSelectedItem();
				if(item != null)
				{
					gameController.executeDropAction(item);
				}
				else
				{
					JOptionPane.showMessageDialog(null, Constants.MESSAGE_NO_ITEM_SELECTED);
				}
						
			}
		});
	}

	@Override
	public void raiseError(String msg) 
	{

	}

	@Override
	public void communicationHelp(String help) 
	{

	}

	@Override
	public void engineOff(boolean atShip)
	{

	}

	@Override
	public void communicationCompleted()
	{
	
	}

	@Override
	public void robotUpdate(int fuel, int recycledMaterial)
	{

	}

	@Override
	public void robotSays(String message) 
	{

	}
	
}

